Imports Phoenix.DataAccess
Imports Phoenix.Events



Namespace Helpers

    Public Class ClientHelper : Inherits BaseHelper

#Region " BaseHelper Overrides "
        Private Shared mInstance = New ClientHelper
        Public Shared ReadOnly Property Instance() As ClientHelper
            Get
                Return mInstance
            End Get
        End Property


        Private Sub New()

        End Sub

#End Region

#Region " CRUD Methods "
        Private Shared Sub UpdateClientCore(ByVal client As DataSetData.Client)
            If client.RowState = DataRowState.Detached Then
                AppDataLayer.Clients.AddClient(client)
            End If
            AppDataLayer.UpdateClient(client)
        End Sub

        Private Shared Sub DeleteClientCore(ByVal obj As DataSetData.Client)
            obj.Delete()
            AppDataLayer.UpdateClient(obj)
        End Sub
#End Region

#Region " Edit Commands "
        Public Shared Function CreateNewClient() As DataSetData.Client
            Dim cl As DataSetData.Client = AppDataLayer.Clients.NewClient
            cl.FullName = Res.NewClientDefaultName
            Return cl
        End Function


        Public Shared Function Copy(ByVal obj As DataSetData.Client) As DataSetData.Client
            Dim s As DataSetData.Client = AppDataLayer.Clients.NewClient
            s = Copy(s, obj)
            s.FullName = "Copy of " & s.FullName
            Return s
        End Function

        Private Shared Function Copy(ByVal s As DataSetData.Client, ByVal obj As DataSetData.Client) As DataSetData.Client
            s.FullName = obj.FullName
            s.Address = obj.Address
            s.BirthDate = obj.BirthDate
            s.Comment = obj.Comment
            s.InOffice = obj.InOffice
            s.Phone = obj.Phone
            s.Email = obj.Email
            s.MasterLevel = obj.MasterLevel
            Return s
        End Function

        Public Shared Sub UpdateClient(ByVal client As DataSetData.Client, ByVal sender As Object)
            UpdateClient(client, sender, False)
        End Sub

        Public Shared Sub UpdateClient(ByVal client As DataSetData.Client, ByVal sender As Object, ByVal ignoreDuplication As Boolean)
            Dim e As PhoenixEventArgs

            If AppConfig.WarningClientDuplicateNames And (client.RowState <> DataRowState.Deleted) And (Not ignoreDuplication) Then
                If CheckDuplicateNames(client) Then
                    If AppMessageBox(Res.ClientDuplicateName, MsgBoxStyle.Exclamation Or MsgBoxStyle.YesNo) = MsgBoxResult.No Then
                        Throw New CancelOperationException
                    End If
                End If
            End If

            If client.RowState = DataRowState.Detached Then
                e = New PhoenixEventArgs(client, PhoenixEventCode.BizAdded)
            Else
                e = New PhoenixEventArgs(client, PhoenixEventCode.BizUpdated)
            End If

            UpdateClientCore(client)
            AppRaiseEvent(sender, e)
        End Sub


        Private Shared Function CheckDuplicateNames(ByVal client As DataSetData.Client) As Boolean
            For Each item As DataSetData.Client In AppDataLayer.Clients
                If item.ClientID <> client.ClientID Then
                    If String.Compare(item.FullName, client.FullName, True) = 0 Then
                        Return True
                    End If
                End If
            Next

            Return False
        End Function


        Public Shared Sub DeleteClient(ByVal obj As DataSetData.Client, ByVal sender As Object)
            DeleteClient(obj, sender, True)
        End Sub

        Public Shared Sub DeleteClient(ByVal obj As DataSetData.Client, ByVal sender As Object, ByVal confirm As Boolean)
            If CloseForm(obj) Then
                If confirm Then
                    If Not AppConfig.SilenceClientDeletion Then
                        Dim result As MsgBoxResult = AppMessageBox(String.Format(Res.ClientDeleteQuestion, obj.FullName), MsgBoxStyle.YesNo Or MsgBoxStyle.Exclamation)
                        If result <> MsgBoxResult.Yes Then Return
                    End If
                End If

                AppRaiseEvent(sender, New PhoenixEventArgs(obj, PhoenixEventCode.BizRemoved))
                Try
                    DeleteClientCore(obj)
                Catch
                    AppRaiseEvent(sender, New PhoenixEventArgs(obj, PhoenixEventCode.BizAdded))
                End Try
            End If
        End Sub

        Public Shared Sub DeleteClients(ByVal list As IList, ByVal sender As Object)
            If list.Count = 1 Then
                DeleteClient(list(0), sender, True)
            Else
                DeleteClients(list, sender, True)
            End If
        End Sub

        Public Shared Sub DeleteClients(ByVal list As IList, ByVal sender As Object, ByVal confirm As Boolean)
            If (Not AppConfig.SilenceClientsDeletion) And confirm Then
                Dim result As MsgBoxResult = AppMessageBox(String.Format(Res.ClientsDeleteQuestion, list.Count), MsgBoxStyle.YesNo Or MsgBoxStyle.Exclamation)
                If result <> MsgBoxResult.Yes Then Return
            End If

            OperationCanceled = False
            Dim mylist(list.Count - 1) As DataSetData.Client
            list.CopyTo(mylist, 0)
            For i As Long = 0 To list.Count - 1
                DeleteClient(mylist(i), sender, False)
                If OperationCanceled Then Return
            Next
        End Sub


        'Public Shared Sub Delete(ByVal obj As DataSetData.Client, ByVal sender As Object)

        '    Dim ds As DataAccess.DataSetAppointments = AppDataLayer.GetClientAppointments(obj.ClientID)

        '    While ds.Appointments.Count > 0
        '        AppointmentHelper.Delete(ds.Appointments(0), sender)
        '    End While

        '    obj.Delete()
        '    Helpers.ClientHelper.UpdateClient(obj, sender)
        'End Sub
#End Region

#Region " Others Helpers "
        Public Shared Sub SwitchInOfficeStatus(ByVal client As DataSetData.Client, ByVal sender As Object)
            If client Is Nothing Then Return
            Try
                AppCursor.SetBusy()
                UpdateClient(client, sender, True)
            Finally
                AppCursor.SetFree()
            End Try
        End Sub

#End Region

    End Class
End Namespace
